<?php
class Model {
	protected $name;
	protected $data = array();

	# TODO: konvertera klassnamn till tabellnamn, plural och singular
	public function __construct ($data) {
		$this->name = get_class($this);
		$this->data = $data;
	}

	public function asArray () {
		return $this->data;
	}

	public function __call ($method, $args) {
		$type	= substr($method, 0, 3);
		$k		= strtolower(ccFix(substr($method, 3), '_'));

		if ($type == 'get') {
			if (isset($this->data[$k])) {
				return $this->data[$k];
			}

			return null;
		}
		elseif ($type == 'set') {
			$this->data[$k] = $args[0];

			return $args[0];
		}

		die("method $method does not exiiiiissssssssst");
	}

	# TODO: använd $this->name och pluralisera det
	public function save () {
		if (isset($this->data['users_id'])) {
			UsersModel::update($this->data['users_id'], $this->data);
		}
		else {
			UsersModel::insert($this->data);
		}
	}
}
